package com.appkubes.mall.admin.application.customer.customer.service.impl;

import com.appkubes.common.utils.data.string.UUIDUtils;
import com.appkubes.mall.admin.application.admin.admin.service.AdminService;
import com.appkubes.mall.admin.application.admin.binding.service.AdminBindingService;
import com.appkubes.mall.admin.application.customer.customer.entity.dto.CustomerInfoDTO;
import com.appkubes.mall.admin.application.customer.customer.entity.vo.LoginByTelVO;
import com.appkubes.mall.admin.application.customer.customer.entity.vo.RegisterByTelVO;
import com.appkubes.mall.admin.application.customer.customer.service.CustomerService;
import com.appkubes.mall.core.entity.MallAdmin;
import com.appkubes.mall.core.entity.MallAdminBinding;
import com.appkubes.mall.core.entity.MallCustomer;
import com.appkubes.mall.core.service.impl.BaseMallCustomerServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @Author: Rongjie
 * @Date: 2020/3/4
 * @Description:
 */
@Service
public class CustomerServiceImpl extends BaseMallCustomerServiceImpl implements CustomerService {

    @Autowired
    AdminService adminService;

    @Autowired
    AdminBindingService adminBindingService;

    @Override
    public MallAdminBinding loginByTel(LoginByTelVO loginByTel) {
        MallAdmin admin = new MallAdmin();
        admin.setUserName(loginByTel.getTel());
        admin.setPassword(loginByTel.getPassword());
        admin = adminService.getOneByEntity(admin);
        MallAdminBinding adminBinding = new MallAdminBinding();
        adminBinding.setAdminKey(admin.getAdminKey());
        return adminBindingService.getOneByEntity(adminBinding);
    }

    @Override
    @Transactional
    public MallAdminBinding registerByTel(RegisterByTelVO registerByTel) {
        MallAdmin admin = new MallAdmin();
        admin.setUserName(registerByTel.getTel());
        admin.setPassword(registerByTel.getPassword());
        adminService.save(admin);

        MallAdminBinding adminBinding = new MallAdminBinding();
        adminBinding.setAdminKey(admin.getAdminKey());
        adminBinding.setCustomerKey(UUIDUtils.getUUID());
        adminBindingService.save(adminBinding);
        return adminBinding;
    }
}
